/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Entities;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Sarah
 */
@Stateless
public class UserlistFacade extends AbstractFacade<Userlist> {
    @PersistenceContext(unitName = "IServer-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public UserlistFacade() {
        super(Userlist.class);
    }

    public String login(String id, String pwd) {
        String result = "NO";
        for (Userlist u : findAll()) {
            if (u.getuId().equals(id)) {
                if (u.getPwd().equals(pwd)) {
                    result = "YES";
                }
            }
        }
        return result;
    }

    public String join(String id, String pwd, String name) {
        String result = "YES";
        Boolean flag = true;
        for (Userlist u : findAll()) {
            if (u.getuId().equals(id)) {
                result = "NO";
                flag = false;
            }
        }
        if (flag) {
            Userlist u = new Userlist();
            u.setuId(id);
            u.setPwd(pwd);
            u.setName(name);
            u.setCash(0);
            create(u);
        }
        return result;
    }

    public void addCash(String id, int cash) {
        for (Userlist u : findAll()) {
            if (u.getuId().equals(id)) {
                u.addCash(cash);
            }
        }
    }
}
